﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using IESN.DAL;
using IESN.BaseForms;

namespace IESN
{
    public partial class ProcessManagerFrm : EditBaseFrm
    {
        private ProcessDetail _processDetail = new ProcessDetail();

        public ProcessManagerFrm()
        {
            InitializeComponent();
            CurrentEntity = new ProcessInfo();
        }

        /// <summary>
        /// 初始化控件
        /// </summary>
        protected override void InitControl()
        {
            //base.InitControl();
            //ControlHelper.SetControlEnable(panelContent, true);

            cbbProcessNo.Enabled = true;
            txtProcessNo.Visible = false;
            //txtRemark.ReadOnly = true;
        }

        /// <summary>
        /// 获取数据集
        /// </summary>
        /// <returns></returns>
        protected override DataSet GetDataSet()
        {
            string detailsSQL = "Select d.*,s.description From Process_detail d left join stage s on d.StageNo=s.StageNo order by d.step";
            DataSet detailsDs = AccessDBHelper.Query(detailsSQL);

            _ds = AccessDBHelper.GetDataSetByTableName(_currentEntity.TableName);

            if (detailsDs.Tables != null && detailsDs.Tables.Count > 0)
            {
                DataTable detailsTable = detailsDs.Tables[0].Copy();
                _ds.Tables.Add(detailsTable);
            }
                
            return _ds;
        }

        /// <summary>
        /// 设置界面值
        /// </summary>
        protected override void SetFormValue()
        {
            cbbProcessNo.DataSource = _ds.Tables[0];
            cbbProcessNo.ValueMember = "ID";
            cbbProcessNo.DisplayMember = "ProcessNo";
        }

        protected override void SetDefaultValue()
        {
            base.SetDefaultValue();
            BindingSource bs = (BindingSource)dgvProcessDetails.DataSource;
            DataTable dt= bs.DataSource as DataTable;
            dt.Rows.Clear();

            dgvProcessDetails.Refresh();

            txtProcessNo.Visible = true;
            cbbProcessNo.Visible = false;
            panelList.Enabled = true;
            dgvProcessDetails.ReadOnly = false;
        }

        /// <summary>
        /// Combox切换
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void cbbProcessNo_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(cbbProcessNo.ValueMember))
            {
                DataRow[] drProcess= _ds.Tables[0].Select("ID='" + cbbProcessNo.SelectedValue + "'");
                if (drProcess != null && drProcess.Length > 0)
                {
                    txtRemark.Text = drProcess[0]["Remark"].ToString();
                }

                DataTable newDt = _ds.Tables[1].Clone();
                DataRow[] drs = _ds.Tables[1].Select("ProcessID='" + cbbProcessNo.SelectedValue + "'");

                if (drs!=null && drs.Length > 0)
                {
                    newDt = drs.CopyToDataTable();
                    BindingSource bs = new BindingSource();
                    bs.DataSource = newDt;
                    dgvProcessDetails.DataSource = bs;
                }
            }
        }


    }
}
